class Movie < ActiveRecord::Base

  validates_presence_of   :title, :description
  validates_uniqueness_of :title
  validates_length_of     :description, :minimum => 10
  validates_inclusion_of  :rating, :in => %w{ G PG PG-13 R NC-17 }

def appropriate_for_birthdate?(birthdate)
  if %w{ R NC-17 }.include?(rating)
    (Time.now - 17.years) > birthdate
  elsif rating == "PG-13"
    (Time.now - 13.years) > birthdate
  else
    true
  end
end

def self.find_all_appropriate_for_birthdate(bd)
  movies = Movie.find(:all).select { |m| m.appropriate_for_birthdate?(bd) }
end

end
